package dao.impl;

import Service.ICategoryService;
import Service.IProductService;
import Service.impl.CategoryServiceImpl;
import Service.impl.ProductServiceImpl;
import dao.ICategoryDao;
import dao.IProductDao;
import dao.ISupplierDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.*;

import java.net.PortUnreachableException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    @Override
    public void addProduct(Product product) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="insert into product(productname,price,description,count,categoryid,supplier_id) values(?,?,?,?,?,?)";
        try {
            queryRunner.update(FirstApp.getConn(),sql,
                    product.getProductName(),product.getPrice(),product.getDescription(), product.getCount(),product.getCategory().getCategoryid()
            ,product.getSupplier().getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateProduct(Product product) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update product set productname=?,price=?,description=?,count=?,categoryid=?,supplier_id=? where productid=?";
        try {
            queryRunner.update(FirstApp.getConn(),sql,
                    product.getProductName(),product.getPrice(),product.getDescription(),product.getCount(),product.getCategory().getCategoryid(),product.getProductid()
            ,product.getSupplier().getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteProduct(int id) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="delete from product where productid=?";
        try {
            queryRunner.update(FirstApp.getConn(),sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Product findById(int id) {
        Product product=null;
        ICategoryDao categoryDao=new CategoryDaoImpl();
        ISupplierDao supplierDao=new SupplierDaoImpl();
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from product where productid=?";
        try {
            Object[] query=queryRunner.query(FirstApp.getConn(),sql,new ArrayHandler(),id);
            if(query.length>0)
            {
                product=new Product();
                product.setProductid((Integer) query[0]);
                product.setProductName((String) query[1]);
                product.setPrice((Double) query[2]);
                product.setDescription((String) query[3]);
                product.setCount((Integer) query[4]);
                Category category=categoryDao.findById((Integer) query[5]);
                Supplier supplier=supplierDao.findByid((Integer) query[6]);
                product.setCategory(category);
                product.setSupplier(supplier);
                return product;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> findAll() {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = null;
        List<Product> productList=new ArrayList<>();
        IProductService productService=new ProductServiceImpl();
        ICategoryService categoryService=new CategoryServiceImpl();
        ISupplierDao supplierDao=new SupplierDaoImpl();
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "select * from product";
            List<Object[]> objects= queryRunner.query(FirstApp.getConn(), sql, new ArrayListHandler());
            for(int i=0;i<objects.size();i++)
            {
                Product product=new Product();
                product.setProductid((Integer) objects.get(i)[0]);
                product.setProductName((String) objects.get(i)[1]);
                product.setPrice((Double) objects.get(i)[2]);
                product.setDescription((String) objects.get(i)[3]);
                product.setCount((Integer) objects.get(i)[4]);
                product.setCategory(categoryService.findById((Integer) objects.get(i)[5]));
                product.setSupplier(supplierDao.findByid((Integer) objects.get(i)[6]));
                productList.add(product);
            }
            connection.commit();
            return productList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> findByLikeName(String name) {
        List<Product> product=null;
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from product where productname like ?";
        name="%"+name+"%";
        try {
            product=queryRunner.query(FirstApp.getConn(),sql,new BeanListHandler<>(Product.class),name);
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
